<?php

namespace App\Http\Controllers\Auth;

use App\Models\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use App\Rules\CheckMobile;
use App\Rules\SecuriytCode;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        $message = [
            'name.required' => '请输入用户名',
            'name.unique' => '用户名已存在',
            'name.string' => '用户名数据格式错误',
            'name.min' => '用户名不能小于6位',
            'name.max' => '用户名不能大于20位',
            'telephone.required' => '请输入手机号码',
            'telephone.unique' => '该手机号码已被绑定',
            'password.required' => '请输入密码',
            'password.string' => '数据格式错误',
            'password.min' => '密码不能小于6位',
            'password.max' => '密码不能大于20位',
            'password.confirmed' => '两次输入的密码不一致',
            'code.required' => '请输入手机验证码',
        ];

        return Validator::make($data, [
            'name' => 'required|unique:users|string|min:6|max:20',
        	'telephone' => ['required', 'unique:users', new CheckMobile()],
            'password' => 'required|string|min:6|max:20|confirmed',
        	'code' => ['required',new SecuriytCode($data['telephone'])]
        ], $message);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
        	'telephone' => $data['telephone'],
        	'email'=>'',
            'password' => bcrypt($data['password']),
        ]);
    }
    
    public function showRegistrationForm()
    {
    	return view('auth.register');
    }
}
